package com.app.shortvideo.config

import android.os.Environment
import com.app.shortvideo.BuildConfig
import com.blankj.utilcode.util.SPUtils
import java.io.File

object Constant {

    const val BASE_URL = "http://api.biubiubiu.mobi"
//    const val BASE_URL_TEST = "http://192.168.0.4:8089/"

    const val BASE_URL_TEST = "http://api.biubiubiu.mobi"
    const val BASE_URL_LOCAL = "http://192.168.0.4:8089/"


    const val SERVER_PRO = 0
    const val SERVER_TEST = 1
    const val SERVER_LOCAL = 2

    const val PIXIE_CHAIN_ID = "1"

    private val ROOTPAHT = Environment.getExternalStorageDirectory()
        .absolutePath + File.separator + Environment.DIRECTORY_DOWNLOADS + File.separator

    private val IMAGE_DIR: String = ROOTPAHT + "shortvideo/image" + File.separator
    private val VIDEO_DIR: String = ROOTPAHT + "shortvideo/video" + File.separator
    private val CACHE_DIR: String = ROOTPAHT + "shortvideo/cache" + File.separator
    private val TEMP_DIR: String = ROOTPAHT + "shortvideo/temp" + File.separator


    //1,android
    const val PLATFORM = "platform"
    const val DEVICE_ID = "device_id"
    const val VERSION = "app_version"

    const val SP_GAO_DE_AGREEMENT = "SP_GAO_DE_AGREEMENT"
    const val SP_GAO_DE_CITY_CODE = "SP_GAO_DE_CITY_CODE"
    const val SP_GAO_DE_CITY_LATITUDE = "SP_GAO_DE_CITY_LATITUDE"
    const val SP_GAO_DE_CITY_LONGITUDE = "SP_GAO_DE_CITY_LONGITUDE"

    fun getImageDirPath(): String {
        val imageDir: File = File(IMAGE_DIR)
        if (!imageDir.exists()) {
            imageDir.mkdirs()
        }
        return IMAGE_DIR
    }

    fun getCacheDirPath(): String {
        val dir: File = File(CACHE_DIR)
        if (!dir.exists()) {
            dir.mkdirs()
        }
        return CACHE_DIR
    }

    fun getTempDirPath(): String {
        val dir: File = File(TEMP_DIR)
        if (!dir.exists()) {
            dir.mkdirs()
        }
        return TEMP_DIR
    }
    fun getServer(): String {
        return when (getServerType()) {
            SERVER_LOCAL -> getLocalIp()
            SERVER_TEST -> BASE_URL_TEST
            else -> BASE_URL
        }
    }

    fun setServerType(type: Int) {
        SPUtils.getInstance().put(SPConst.KEY_SERVER, type)
    }

    fun getServerType(): Int {
        return when (BuildConfig.SERVER_TYPE) {
            "1" -> {
                SPUtils.getInstance().getInt(SPConst.KEY_SERVER, SERVER_TEST)
            }
            "2" -> {
                SERVER_TEST
            }
            else -> {
                SERVER_PRO
            }
        }
    }
    fun getLocalIp(): String {
        val url = SPUtils.getInstance().getString(SPConst.KEY_SERVER_LOCAL)
        return if (url.isNullOrEmpty()) {
            BASE_URL_LOCAL
        } else {
            url
        }
    }


    fun setLocalIp(ip: String) {
        SPUtils.getInstance().put(SPConst.KEY_SERVER_LOCAL, ip)
    }
}